package com.smart.community.property.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 物业费统计视图对象
 * @author Wu.Liang
 * @since 2025-06-26
 * @version 1.0.0
 */
@Data
public class PropertyFeeStatisticsVO implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 社区ID */
    private Long communityId;

    /** 社区名称 */
    private String communityName;

    /** 统计周期 */
    private String statisticsPeriod;

    /** 统计开始日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate startDate;

    /** 统计结束日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate endDate;

    /** 总账单数 */
    private Integer totalBills;

    /** 已缴账单数 */
    private Integer paidBills;

    /** 未缴账单数 */
    private Integer unpaidBills;

    /** 逾期账单数 */
    private Integer overdueBills;

    /** 总金额 */
    private BigDecimal totalAmount;

    /** 已缴金额 */
    private BigDecimal paidAmount;

    /** 未缴金额 */
    private BigDecimal unpaidAmount;

    /** 平均缴费金额 */
    private BigDecimal averageAmount;

    /** 缴费率 */
    private Double paymentRate;

    /** 收缴率 */
    private BigDecimal collectionRate;

    /** 逾期率 */
    private Double overdueRate;

    /** 费用类型分布 */
    private List<FeeTypeDistribution> feeTypeDistribution;

    /** 缴费方式分布 */
    private List<PaymentMethodDistribution> paymentMethodDistribution;

    /** 月度统计 */
    private List<MonthlyStatistics> monthlyStatistics;

    /**
     * 费用类型分布
     */
    @Data
    public static class FeeTypeDistribution {
        /** 费用类型 */
        private Integer feeType;

        /** 费用类型名称 */
        private String feeTypeName;

        /** 账单数量 */
        private Integer billCount;

        /** 金额 */
        private BigDecimal amount;

        /** 占比 */
        private Double percentage;
    }

    /**
     * 缴费方式分布
     */
    @Data
    public static class PaymentMethodDistribution {
        /** 缴费方式 */
        private String paymentMethod;

        /** 缴费方式名称 */
        private String paymentMethodName;

        /** 账单数量 */
        private Integer billCount;

        /** 金额 */
        private BigDecimal amount;

        /** 占比 */
        private Double percentage;
    }

    /**
     * 月度统计
     */
    @Data
    public static class MonthlyStatistics {
        /** 月份 */
        private String month;

        /** 账单数量 */
        private Integer billCount;

        /** 金额 */
        private BigDecimal amount;

        /** 占比 */
        private Double percentage;
    }

    /**
     * 执行所有计算
     */
    public void calculateAll() {
        calculateUnpaidBills();
        calculateUnpaidAmount();
        calculatePaymentRate();
        calculateCollectionRate();
        calculateOverdueRate();
        calculateAverageAmount();
    }

    /**
     * 计算未缴账单数
     */
    public void calculateUnpaidBills() {
        if (totalBills != null && paidBills != null) {
            this.unpaidBills = totalBills - paidBills;
        } else {
            this.unpaidBills = 0;
        }
    }

    /**
     * 计算未缴金额
     */
    public void calculateUnpaidAmount() {
        if (totalAmount != null && paidAmount != null) {
            this.unpaidAmount = totalAmount.subtract(paidAmount);
        } else {
            this.unpaidAmount = BigDecimal.ZERO;
        }
    }

    /**
     * 计算缴费率
     */
    public void calculatePaymentRate() {
        if (totalBills != null && totalBills > 0 && paidBills != null) {
            this.paymentRate = (double) paidBills / totalBills * 100;
        } else {
            this.paymentRate = 0.0;
        }
    }

    /**
     * 计算收缴率
     */
    public void calculateCollectionRate() {
        if (totalAmount != null && totalAmount.compareTo(BigDecimal.ZERO) > 0 && paidAmount != null) {
            this.collectionRate = paidAmount.divide(totalAmount, 4, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal("100"));
        } else {
            this.collectionRate = BigDecimal.ZERO;
        }
    }

    /**
     * 计算逾期率
     */
    public void calculateOverdueRate() {
        if (totalBills != null && totalBills > 0 && overdueBills != null) {
            this.overdueRate = (double) overdueBills / totalBills * 100;
        } else {
            this.overdueRate = 0.0;
        }
    }

    /**
     * 计算平均缴费金额
     */
    public void calculateAverageAmount() {
        if (paidBills != null && paidBills > 0 && paidAmount != null) {
            this.averageAmount = paidAmount.divide(new BigDecimal(paidBills), 2, BigDecimal.ROUND_HALF_UP);
        } else {
            this.averageAmount = BigDecimal.ZERO;
        }
    }

    /**
     * 获取缴费率百分比字符串
     */
    public String getPaymentRatePercent() {
        if (paymentRate != null) {
            return String.format("%.2f%%", paymentRate);
        }
        return "0.00%";
    }

    /**
     * 获取收缴率百分比字符串
     */
    public String getCollectionRatePercent() {
        if (collectionRate != null) {
            return collectionRate.setScale(2, BigDecimal.ROUND_HALF_UP) + "%";
        }
        return "0.00%";
    }

    /**
     * 获取逾期率百分比字符串
     */
    public String getOverdueRatePercent() {
        if (overdueRate != null) {
            return String.format("%.2f%%", overdueRate);
        }
        return "0.00%";
    }
}
